package com.admin.util;

import java.io.UnsupportedEncodingException;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONObject;

import com.admin.dao.unicode;
import com.cxtj.CxBean;
import com.cxtj.CxtjBean;
import com.peersafe.chainsql.core.Chainsql;
import com.peersafe.chainsql.core.Submit.SyncCond;

public class ChainUtil {
	public static final Chainsql c = Chainsql.c;
	static final String chainTable="mgsjsql";

	public void connect() {
		c.connect("ws://192.168.0.219:6006");
		c.as("raJ3ti7h82oG9gi3RYwbWWB42WsnubLabo", "shMZ3HTU1eCqTYtVNnD89FABsqqRq");
	}
	
	/**
	 * author wg
	 * 插入数据到区块链
	 * @param CxtjBean cx
	 * @return String
	 */
	public  String insert2Chain(CxBean cx) {
		connect();
		String sql="{"    
				+ "'consql':'"+cx.getConsql()+"',"
				+ "'sqluser':'"+cx.getSqluser()+ "',"
				+ "'sjnumber':"+cx.getSjnumber()+","
			//	+ "'dycs':"+cx.getDycs()+","
				+ "'czsj':'"+cx.getCzsj().toString()+"'"
				+ "}";
		JSONObject json = c.table(chainTable).insert(c.array(sql)).submit(SyncCond.db_success);
		return json.getString("status");
	}

	/**
	 * title:批量插入数据到区块链
	 * author:wg
	 * @param cxList
	 * @return
	 */	
	public   String insertList2Chain(List<CxBean> cxList) {
		connect();
		List<String>  as=new ArrayList<String>();
		for(CxBean cx:cxList){
			String sql="{"    
					+ "'consql':'"+unicode.string2Unicode(cx.getConsql())+"',"
					+ "'sqluser':'"+cx.getSqluser()+ "',"
					+ "'sjnumber':"+cx.getSjnumber()+","
					+ "'OBJ_NAME':"+cx.getOBJ_NAME()+","
					+ "'czsj':'"+cx.getCzsj().toString()+"'"
					+ "}";
			as.add(sql);
		}				
	  	System.out.println(as.get(0));  
	  	JSONObject jsList = c.table(chainTable).insert(as).submit(SyncCond.db_success); 
	//	JSONObject json = c.table(chainTable).insert(c.array(sql)).submit(SyncCond.db_success);
		return jsList.getString("status");
	}
	
	public  void select() {		
	    JSONObject js01 = c.table(chainTable).get(c.array("")).submit(SyncCond.db_success); //.limit("{'total':10}")
		System.out.println(js01);
	}
	
/*	public static void main(String[] args) throws UnsupportedEncodingException, SQLException {
		ChainUtil cu = new ChainUtil();
		String res = cu.insertList2Chain(OrcUtil.getCxList(null,null));
		System.out.println(res);
	}*/
}
